<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: group_prices.server.php 52 2013-09-21 11:57:31Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 52 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:57:31 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

$xajax->registerFunction('updateProducts');
$xajax->registerFunction('staffelAdd');
$xajax->registerFunction('staffelCancel');
$xajax->registerFunction('staffelCancelTemplate');
$xajax->registerFunction('staffelSave');
$xajax->registerFunction('firstLoad');
$xajax->registerFunction('staffelDelete');
$xajax->registerFunction('staffelEdit');
$xajax->registerFunction('staffelSaveEdit');
$xajax->registerFunction('deleteTemplate');
$xajax->registerFunction('saveTemplate');
$xajax->registerFunction('editTemplate');
$xajax->registerFunction('saveTemplateValues');
$xajax->registerFunction('editTemplateValues');
$xajax->registerFunction('loadTemplate');
$xajax->registerFunction('loadTemplateValues');

/*
Initialisierung
*/
function firstLoad($pid) {
	$ajax = new xajaxResponse();
	$ajax = buildTemplates($ajax,$pid);
	return update_staffel_table($pid,$ajax);
}
//**********************************************************
//**********************Profilbereich***********************
//**********************************************************
/*
Abbrechen bei Profileingaben
*/
function staffelCancelTemplate($StaffelId)
{
	//echo $aFormValues;
	$ajax = new xajaxResponse();
	$ajax->assign("staffel_add_".$StaffelId,"innerHTML",'');
	$ajax->assign('eingabebox_'.$StaffelId, 'style.display', 'none');
	$ajax->assign('editbox_'.$StaffelId, 'style.display', 'none');
	$ajax->assign("fehlermeldung_".$StaffelId,"innerHTML",'');
	$ajax->assign("load_".$StaffelId,"innerHTML",'');
	return $ajax;
}
/*
Initialisierung der Profileingabe
*/
function buildTemplates($ajax,$pid) {
	global $db;

	$i = 0;
	$group_values = $db->db_query("SELECT
										customers_status_image,
										customers_status_id,
										customers_status_name
									FROM
										".TABLE_CUSTOMERS_STATUS."
									WHERE
										language_id = '".$_SESSION['languages_id']."' AND customers_status_id != '0'");
	while(!$group_values->EOF) {
		$i++;
		$group_data[$i] = array('STATUS_NAME' => $group_values->fields['customers_status_name'],
								'STATUS_IMAGE' => $group_values->fields['customers_status_image'],
								'STATUS_ID' => $group_values->fields['customers_status_id']);
		$group_values->MoveNext();
	}
	for ($col = 0, $n = sizeof($group_data); $col < $n +1; $col ++) {
		if ($col != 0) {
			$tag ='';
			$tag .='<div>';
			$tag .= draw_pull_down_menu('template_drop',buildAllTemplatesDropDown(),(0 == $selectedTemplate) ? '0' : $selectedTemplate,'id="template_drop" class="profil"');
			$tag .='&nbsp;';
			$tag .='<span class="cursor">';
			$tag .=image('images/profile_load.gif',PROFILE_WILL_LOAD,'','','onclick="xajax_loadTemplate('.$group_data[$col]['STATUS_ID'].',document.getElementById(\'template_drop\').value,'.$pid.');"');
			$tag .='</span>';
			$tag .='&nbsp;';
			$tag .='<span class="cursor">';
			$tag .=image('images/profile_save.gif',PROFILE_WILL_SAVE,'','','onclick="xajax_saveTemplate('.$group_data[$col]['STATUS_ID'].');"');
			$tag .='</span>';
			$tag .='&nbsp;';
			$tag .='<span class="cursor">';
			$tag .=image('images/profile_edit.gif',PROFILE_WILL_RENAME,'','','onclick="document.getElementById(\'templatename_edit_'.$group_data[$col]['STATUS_ID'].'\').value=\'\';xajax_editTemplate('.$group_data[$col]['STATUS_ID'].',document.getElementById(\'template_drop\').value);"');
			$tag .='</span>';
			$tag .='&nbsp;';
			$tag .='<span class="cursor">';
			$tag .=image('images/icons/minus-circle.png',PROFILE_WILL_DELETE,'','','onclick="xajax_deleteTemplate(document.getElementById(\'template_drop\').value,'.$pid.','.$group_data[$col]['STATUS_ID'].');"');
			$tag .='</span>';
			$tag .='</div>';
			$ajax->assign("profile_".$group_data[$col]['STATUS_ID'],"innerHTML",$tag);
		}
	}
	//echo $tag;
	//$ajax->assign("profile","innerHTML",$tag);
	return $ajax;
}
/*
Profile abspeichern
*/
function saveTemplate($staffelid,$templatename='') {
	$ajax = new xajaxResponse();
	$ajax->assign("staffel_add_".$staffelid,"innerHTML",'');
	$ajax->assign('eingabebox_'.$staffelid, 'style.display', 'inline');
	$ajax->assign('editbox_'.$staffelid, 'style.display', 'none');
	$ajax->assign("fehlermeldung_".$staffelid,"innerHTML",'');
	$ajax->assign("load_".$staffelid,"innerHTML",'');
	return $ajax;
}

function saveTemplateValues($staffelid,$pid,$templatename) {
	global $db;
	$row = $db->db_query("SELECT
								COUNT(template_name) AS anzahl
							FROM
								".TABLE_STAFFEL_TEMPLATES."
							WHERE
								template_name = '".$templatename."'");

	$ajax = new xajaxResponse();
	if($row->fields['anzahl'] == '0') {
		$data = array('template_name' => $templatename);
		$db->db_perform('staffel_templates', $data);
		$newTemplateId = $db->db_insert_id;

		$staffel_values = $db->db_query("SELECT
	                                         products_id,
	                                         quantity,
	                                         personal_offer
	                                     FROM
	                                         ".TABLE_PERSONAL_OFFERS_BY.$staffelid."
	                                     WHERE
	                                         products_id = '".$pid."' AND quantity != 1
	                                     ORDER BY quantity ASC");

		while(!$staffel_values->EOF) {
			$data = array(	'template_id' => $newTemplateId,
							'quantity' => $staffel_values->fields['quantity'],
							'personal_offer' => $staffel_values->fields['personal_offer']);
	  		$db->db_perform(TABLE_STAFFEL_TO_TEMPLATES, $data);
	  		$staffel_values->MoveNext();
		}
		$ajax = buildTemplates($ajax,$pid);
		$ajax->assign('eingabebox_'.$staffelid, 'style.display', 'none');
		$ajax->assign('editbox_'.$staffelid, 'style.display', 'none');
		$fehlermeldung = STAFFEL_ERROR_MESSAGE_9_0.$templatename.STAFFEL_ERROR_MESSAGE_9_1;
	} else
		$fehlermeldung = STAFFEL_ERROR_MESSAGE_10_0.$templatename.STAFFEL_ERROR_MESSAGE_10_1;
	
	$ajax->assign("fehlermeldung_".$staffelid,"innerHTML",$fehlermeldung);
	return $ajax;
}

/*
Profile bearbeiten
*/
function editTemplate($staffelid,$templateid) {
	$ajax = new xajaxResponse();
	if ($templateid == '0') {
		$fehlermeldung .= STAFFEL_ERROR_MESSAGE_11;
		$ajax->assign("fehlermeldung_".$staffelid,"innerHTML",$fehlermeldung);
	} else {
		$ajax->assign("fehlermeldung_".$staffelid,"innerHTML",'');
		$ajax->assign('editbox_'.$staffelid, 'style.display', 'inline');
		$ajax->assign('editbox_'.$staffelid, 'value', 'yy');
	}
	$ajax->assign('eingabebox_'.$staffelid, 'style.display', 'none');
	$ajax->assign("load_".$staffelid,"innerHTML",'');
	$ajax->assign("staffel_add_".$staffelid,"innerHTML",'');
	return $ajax;
}

function editTemplateValues($staffelid,$pid,$templatename,$templateid) {
	global $db;

	$row = $db->db_query("SELECT
								COUNT(template_name) AS anzahl
							FROM
								".TABLE_STAFFEL_TEMPLATES."
							WHERE
								template_name = '".$templatename."'
							AND
								template_id != '".$templateid."'");

	$ajax = new xajaxResponse();
	if($row->fields['anzahl'] == '0') {
		$data = array('template_name' => $templatename);
		$db->db_perform('staffel_templates', $data, 'update', "template_id = '$templateid'");

		$ajax = buildTemplates($ajax,$pid);
		$ajax->assign('eingabebox_'.$staffelid, 'style.display', 'none');
		$ajax->assign('editbox_'.$staffelid, 'style.display', 'inline');
		$fehlermeldung = STAFFEL_ERROR_MESSAGE_12_0.$templatename.STAFFEL_ERROR_MESSAGE_12_1;
	} else
		$fehlermeldung = STAFFEL_ERROR_MESSAGE_10_0.$templatename.STAFFEL_ERROR_MESSAGE_10_1;

	$ajax->assign("fehlermeldung_".$staffelid,"innerHTML",$fehlermeldung);
	return $ajax;
}

/*
Profile laden
*/
function loadTemplateValues($staffelid,$templateid,$pid) {
	global $db;
	$ajax = new xajaxResponse();
	$db->db_query("DELETE FROM
						".TABLE_PERSONAL_OFFERS_BY.$staffelid."
					WHERE
						products_id = '$pid'");

	$row = $db->db_query("SELECT
								*
							FROM
								".TABLE_STAFFEL_TEMPLATES."
							WHERE
								template_id = '".$templateid."'
							ORDER BY
								quantity, personal_offer");
	while(!$row->EOF) {
		$results[] = $row->fields;
		$row->MoveNext();
	}
	$tag = '';
	foreach($results as $res) {
		$db->db_query("INSERT INTO
							 ".TABLE_PERSONAL_OFFERS_BY.$staffelid."
						SET
							price_id       = '',
							products_id    = '".$pid."',
							quantity       = '".$res['quantity']."',
							personal_offer = '".$res['personal_offer']."'");
	}
	$fehlermeldung .= STAFFEL_ERROR_MESSAGE_13;
	$ajax->assign("fehlermeldung_".$staffelid,"innerHTML",$fehlermeldung);
	$ajax->assign("load_".$staffelid,"innerHTML",'');
	$ajax= update_staffel_table($pid,$ajax);
	return $ajax;
}

function loadTemplate($staffelid,$templateid,$pid) {
	global $db;
	$ajax = new xajaxResponse();
	if ($templateid == '0') {
		$fehlermeldung .= PROFILE_SELECT;
		$ajax->assign("fehlermeldung_".$staffelid,"innerHTML",$fehlermeldung);

	} else {
		$row = $db->db_query("SELECT
									*
								FROM
									".TABLE_STAFFEL_TEMPLATES."
								WHERE
									template_id = '".$templateid."'
								ORDER BY
									quantity, personal_offer");
		while(!$row->EOF) {
			$results[] = $row->fields;
			$row->MoveNext();
		}
		$tag = '';
			$tag .= '<div class="header">';
			$tag .= '<div class="td">'.STAFFEL_QUANTITY.'</div>';
			$tag .= '<div>'.STAFFEL_PRICE.'</div>';
			$tag .= '</div>';
			foreach($results as $res) {
			$tag .= '<div class="column">';
			$tag .= '<div class="td">';
			$tag .= $res['quantity'];
			$tag .= '</div>';
			$tag .= '<div>';
			$tag .= $res['personal_offer'];
			$tag .= '</div>';
			$tag .= '</div>';
		}
		$fehlermeldung = STAFFEL_ERROR_MESSAGE_14;
		$fehlermeldung .= '<div class="clear">'.STAFFEL_ERROR_MESSAGE_15.'';
		$fehlermeldung .= '<span class="cursor"><img src="images/icons/tick.gif" alt="'.PROFILE_CONFIRM.'" title=" '.PROFILE_CONFIRM.' " onclick="xajax_loadTemplateValues('.$staffelid.','.$templateid.','.$pid.');" height="16" width="16"></span><span class="cursor"><img src="images/icons/cross.png"  alt="'.STAFFEL_CANCEL.'" title=" '.STAFFEL_CANCEL.' " onclick="xajax_staffelCancelTemplate('.$staffelid.');" height="16" width="16"></span></div>';
		$ajax->assign("fehlermeldung_".$staffelid,"innerHTML",$fehlermeldung);
		$ajax->assign('editbox_'.$staffelid, 'style.display', 'none');
		$ajax->assign("load_".$staffelid,"innerHTML",$tag);
	}
	$ajax->assign("staffel_add_".$staffelid,"innerHTML",'');
	return $ajax;
}

/*
Profil löschen
*/
function deleteTemplate($templateId,$pid,$staffelid) {
	global $db;
	$ajax = new xajaxResponse();

	$db->db_query("DELETE FROM ".TABLE_STAFFEL_TEMPLATES." WHERE template_id = '$templateId'");
	$db->db_query("DELETE FROM ".TABLE_STAFFEL_TO_TEMPLATES." WHERE template_id = '$templateId'");
	$fehlermeldung = STAFFEL_ERROR_MESSAGE_16;
	$ajax->assign("fehlermeldung_".$staffelid,"innerHTML",$fehlermeldung);
	$ajax->assign("load_".$staffelid,"innerHTML",'');
	$ajax->assign('editbox_'.$staffelid, 'style.display', 'none');
	$ajax->assign('eingabebox_'.$staffelid, 'style.display', 'none');
	$ajax->assign("staffel_add_".$staffelid,"innerHTML",'');
	$ajax = buildTemplates($ajax, $pid);

	return $ajax;
}

/*
Auslesen aller Profildaten
*/
function getAllTemplatesAndGroupPrices() {
	global $db;

	$results = array();
	$arrAllTemplatesAndAttributes = array();
	$row = $db->db_query("SELECT * FROM ".TABLE_STAFFEL_TEMPLATES." st left join ".TABLE_STAFFEL_TO_TEMPLATES." stt using(template_id) ORDER BY st.template_id, stt.quantity, stt.personal_offer");
	while(!$row->EOF) {
		$results[] = $row->fields;
		$row->MoveNext();
	}
	$templateID = null;
	foreach($results as $res) {
		if($templateID != $res['template_id']) {
			$templateID = $res['template_id'];
			$arrAllTemplatesAndAttributes[$templateID]['name'] = $res['template_name'];
		}
		$arrAllTemplatesAndAttributes[$templateID][$res['options_id']][] = $res['option_values_id'];
	}

	return $arrAllTemplatesAndAttributes;
}
function buildAllTemplatesDropDown() {
	$allTemplates = getAllTemplatesAndGroupPrices();
	$returnArray = array(array('id' => '0', 'text' => '-- Profiles --'));
	foreach ($allTemplates as $templateID => $values) {
		$returnArray[] = array('id' => $templateID, 'text' => $values['name']);
	}
	return $returnArray;
}

//**********************************************************
//*********************Staffelbereich***********************
//**********************************************************
/*
Staffeldatenausgabe aktualisieren
*/
function update_staffel_table($pid,$ajax) {
	global $db;

	require_once (DIR_FS_INC.'inc.get_tax_class_id.php');
	$i = 0;
	$group_values = $db->db_query("SELECT
										customers_status_image,
										customers_status_id,
										customers_status_name
									FROM
										".TABLE_CUSTOMERS_STATUS."
									WHERE
										language_id = '".$_SESSION['languages_id']."'
									AND
										customers_status_id != '0'");
	while(!$group_values->EOF) {
		$i++;
		$group_data[$i] = array('STATUS_NAME' => $group_values->fields['customers_status_name'],
								'STATUS_IMAGE' => $group_values->fields['customers_status_image'],
								'STATUS_ID' => $group_values->fields['customers_status_id']);
		$group_values->moveNext();
	}
	for ($col = 0, $n = sizeof($group_data); $col < $n +1; $col ++) {
		if ($col != 0) {
			$tag = '';
			$staffel_values = $db->db_query("SELECT
		                                         products_id,
		                                         quantity,
		                                         personal_offer
		                                     FROM
		                                     	".TABLE_PERSONAL_OFFERS_BY.$group_data[$col]['STATUS_ID']."
		                                     WHERE
		                                         products_id = '".$pid."' AND quantity != 1
		                                     ORDER BY quantity ASC");

				$tag .= '	<div class="titleStaffel">
								<div style="float:left;margin-right:15px">
									'.STAFFEL_TITLE.'
								</div>
								<div style="float:left">
									<span class="cursor">'.image('images/icons/plus-circle.png',STAFFEL_ADD,'','','onclick="xajax_staffelAdd('.$group_data[$col]['STATUS_ID'].','.$pid.', document.getElementById(\'products_price_'.$group_data[$col]['STATUS_ID'].'\').value);"').' 
									</span>
								</div>
								<div>'.STAFFEL_NEW.'</div>
							</div>';
				$tag .= '	<div class="header">';
				$tag .= '		<div class="td">'.STAFFEL_QUANTITY.'</div>';
				$tag .= '		<div class="td">'.STAFFEL_PRICE.'</div>';
				$tag .= '		<div align="center" class="tdSmall">'.STAFFEL_EDIT.'</div>';
				$tag .= '		<div align="center">'.STAFFEL_DELETE.'</div>';
			while(!$staffel_values->EOF) { $i++;
				if (PRICE_IS_BRUTTO == 'true')
					$products_staffel_price = ($staffel_values->fields['personal_offer']*get_tax_rate(get_tax_class_id($pid))/100)+$staffel_values->fields['personal_offer'];
				else
					$products_staffel_price = $staffel_values->fields['personal_offer'];
				
				$tag .= '</div>';
				if($i % 2)
					$tag .= '<div class="tdWide">';
				else
					$tag .= '<div class="tdWide yellowBG">';
					
				$tag .= '<div class="td">';
				$tag .= $staffel_values->fields['quantity'];
				$tag .= '</div>';
				$tag .= '<div class="td">';
				$tag .= round($products_staffel_price, PRICE_PRECISION);
				$tag .= '</div>';
				$tag .= '<div align="center" class="tdSmall">
							<div class="cursor">
								'.image('images/icons/pencil.png', STAFFEL_EDIT,'','','onclick="xajax_staffelEdit('.$group_data[$col]['STATUS_ID'].','.$pid.','.$staffel_values->fields['quantity'].','.round($products_staffel_price, PRICE_PRECISION).');"').'
								<span id="staffel_edit_'.$group_data[$col]['STATUS_ID'].'_'.$pid.'_'.$staffel_values->fields['quantity'].'" name="staffel_edit_'.$group_data[$col]['STATUS_ID'].'_'.$pid.'_'.$staffel_values->fields['quantity'].'">
								</span>
							</div>
						</div>';
				$tag .='<div align="center">
							<span class="cursor">
								'.image('images/icons/minus-circle.png',STAFFEL_DELETE,'','','onclick="xajax_staffelDelete('.$group_data[$col]['STATUS_ID'].','.$pid.','.$staffel_values->fields['quantity'].');"').'
							</span>
						</div>';
				$tag .= '</div>';
				$staffel_values->MoveNext();
			}
			$ajax->assign("staffel_value_".$group_data[$col]['STATUS_ID'],"innerHTML",$tag);
		}
	}
	return $ajax;
}

/*
Staffel löschen
*/
function staffelDelete($staffelid,$pid,$quantity) {
	global $db;
	$ajax = new xajaxResponse();
	// ok, lets check entered data to get rid of user faults
	if ($quantity <= 1)
		$quantity = 2;
	$check = $db->db_query("SELECT
								quantity
							FROM
								".TABLE_PERSONAL_OFFERS_BY.$staffelid."
							WHERE
								products_id = '".$pid."'
							AND
								quantity = '".$quantity."'");

	if($check->_numOfRows < 1)
		$error_message = '<div class="errorMessage">'.STAFFEL_ERROR_MESSAGE_5.'</div>';
		
	else {
		$db->db_query("DELETE FROM
							".TABLE_PERSONAL_OFFERS_BY.$staffelid."
						WHERE
							products_id = '".$pid."'
						AND
							quantity = '".$quantity."'");
									
		$error_message = '<div class="errorMessage">'.STAFFEL_ERROR_MESSAGE_6.'</div>';
		$ajax = update_staffel_table($pid,$ajax);
	}
	$ajax->assign("staffel_add_".$staffelid,"innerHTML",$error_message);
	$ajax->assign('eingabebox_'.$staffelid, 'style.display', 'none');
	$ajax->assign('editbox_'.$staffelid, 'style.display', 'none');
	$ajax->assign("load_".$staffelid,"innerHTML",'');
	$ajax->assign("fehlermeldung_".$StaffelId,"innerHTML",'');
	return $ajax;
}

/*
Staffel editieren
*/
function staffelEdit($staffelid,$pid,$quantity,$price) {
	global $db;
	$ajax = new xajaxResponse();

	$ajax->assign("staffel_add_".$staffelid,"innerHTML",'<div class="innerHTML"><div class="innerInnerHTML"><input name="products_new_staffel_quantity" class="ajax" id="products_new_staffel_quantity" value="'.$quantity.'" size="4" type="text"></div><div class="innerInnerHTMLRight"><input name="products_new_staffel_price" class="ajax" id="products_new_staffel_price" value="'.$price.'" size="8" type="text"></div><div class="innerInnerHTMLRightRight"><span class="cursor"><img src="images/icons/tick.gif" alt="'.STAFFEL_SAVE.'" title=" '.STAFFEL_SAVE.' " onclick="xajax_staffelSaveEdit(document.getElementById(\'products_new_staffel_quantity\').value,document.getElementById(\'products_new_staffel_price\').value,'.$staffelid.','.$pid.','.$quantity.');" height="16" width="16"></span></div><div><span class="cursor"><img src="images/icons/cross.png"  alt="'.STAFFEL_CANCEL.'" title=" '.STAFFEL_CANCEL.' " onclick="xajax_staffelCancel('.$staffelid.','.$pid.','.$quantity.');" height="16" width="16"></span></div></div>');
	$ajax->assign('eingabebox_'.$staffelid, 'style.display', 'none');
	$ajax->assign('editbox_'.$staffelid, 'style.display', 'none');
	$ajax->assign("load_".$staffelid,"innerHTML",'');
	$ajax->assign("fehlermeldung_".$staffelid,"innerHTML",'');
	return $ajax;
}

function staffelSaveEdit($quantity,$staffelpreis,$staffelid,$pid,$quantity_old) {
	global $db;
	require_once (DIR_FS_INC.'inc.get_tax_class_id.php');
	$ajax = new xajaxResponse();
	$error_message = '';
	
	$staffelpreis = str_replace(",", ".", $staffelpreis);

	if(PRICE_IS_BRUTTO == 'true')
		$staffelpreis = ($staffelpreis / (get_tax_rate(get_tax_class_id($pid)) + 100) * 100);

	$staffelpreis = round($staffelpreis, PRICE_PRECISION);

	if($staffelpreis != '' && $quantity != '') {
		$check = $db->db_query("SELECT
									quantity
								FROM
									".TABLE_PERSONAL_OFFERS_BY.$staffelid."
								WHERE
									products_id = '".$pid."'
								AND
									quantity    = '".$quantity_old."'");
		if($check->_numOfRows < 1)
			$error_message = '<div class="errorMessage">'.STAFFEL_ERROR_MESSAGE_7.'</div>';
		else {
			$check = $db->db_query("DELETE FROM
										".TABLE_PERSONAL_OFFERS_BY.$staffelid."
									WHERE
										products_id = '".$pid."'
									AND
										quantity    = '".$quantity_old."'");
		}
		// ok, lets check entered data to get rid of user faults
		if($quantity <= 1)
			$quantity = 2;
		$check = $db->db_query("SELECT
									quantity
								FROM
									".TABLE_PERSONAL_OFFERS_BY.$staffelid."
								WHERE
									products_id = '".$pid."'
								AND
									quantity    = '".$quantity."'");
		// dont insert if same qty!
		if($check->_numOfRows < 1) {
			$db->db_query("INSERT INTO
								".TABLE_PERSONAL_OFFERS_BY.$staffelid."
							SET
								price_id       = '',
								products_id    = '".$pid."',
								quantity       = '".$quantity."',
								personal_offer = '".$staffelpreis."'");

			$error_message = '<div class="errorMessage">'.STAFFEL_ERROR_MESSAGE_8.'</div>';
			$ajax = update_staffel_table($pid,$ajax);

		} else {
			$error_message = '<div class="errorMessage">'.STAFFEL_ERROR_MESSAGE_3.'</div>';
		}
	} else {
		$error_message = '<div class="errorMessage">'.STAFFEL_ERROR_MESSAGE_4.'</div>';
	}
	$ajax->assign("staffel_add_".$staffelid,"innerHTML",$error_message);
	$ajax->assign('eingabebox_'.$staffelid, 'style.display', 'none');
	$ajax->assign('editbox_'.$staffelid, 'style.display', 'none');
	$ajax->assign("load_".$staffelid,"innerHTML",'');
	$ajax->assign("fehlermeldung_".$staffelid,"innerHTML",'');
	return $ajax;
}

/*
Staffelwert hinzufügen
*/
function staffelAdd($StaffelId,$pid,$groupprice) {
	$ajax = new xajaxResponse();
	$products_price = get_group_price($StaffelId, $pid);
	if ($groupprice !='')
		$ajax->assign("staffel_add_".$StaffelId,"innerHTML",'<div class="innerHTML"><div class="innerInnerHTML"><input name="products_new_staffel_quantity" class="ajax" id="products_new_staffel_quantity" value="" size="4" type="text"></div><div class="productsNewStaffelLeft"><input name="products_new_staffel_price" class="ajax" id="products_new_staffel_price" value="" size="8" type="text"></div><div class="productsNewStaffelRight"><span class="cursor"><img src="images/icons/tick.gif" alt="'.STAFFEL_SAVE.'" title=" '.STAFFEL_SAVE.' " onclick="xajax_staffelSave(document.getElementById(\'products_new_staffel_quantity\').value,document.getElementById(\'products_new_staffel_price\').value,'.$StaffelId.','.$pid.');" height="16" width="16"></span></div><div><span class="cursor"><img src="images/icons/minus-circle.png" alt="'.STAFFEL_CANCEL.'" title=" '.STAFFEL_CANCEL.' " onclick="xajax_staffelCancel('.$StaffelId.');" height="16" width="16"></span></div></div>');
	else
		$ajax->assign("staffel_add_".$StaffelId,"innerHTML",'<div class="errorMessage">'.STAFFEL_ERROR_MESSAGE_1.'</div>');
		
	$ajax->assign('eingabebox_'.$StaffelId, 'style.display', 'none');
	$ajax->assign('editbox_'.$StaffelId, 'style.display', 'none');
	$ajax->assign("load_".$StaffelId,"innerHTML",'');
	$ajax->assign("fehlermeldung_".$StaffelId,"innerHTML",'');
	return $ajax;
}
/*
Abbrechen bei Staffelwerteingaben
*/
function staffelCancel($StaffelId) {
	//echo $aFormValues;
	$ajax = new xajaxResponse();
	$ajax->assign("staffel_add_".$StaffelId,"innerHTML",'');
	$ajax->assign('eingabebox_'.$StaffelId, 'style.display', 'none');
	$ajax->assign('editbox_'.$StaffelId, 'style.display', 'none');
	$ajax->assign("load_".$StaffelId,"innerHTML",'');
	$ajax->assign("fehlermeldung_".$StaffelId,"innerHTML",'');
	return $ajax;
}

/*
Speichern von Staffelwerten
*/
function staffelSave($quantity,$staffelpreis,$staffelid,$pid) {
	global $db;
	require_once (DIR_FS_INC.'inc.get_tax_class_id.php');
	$ajax = new xajaxResponse();
	$error_message = '';
	if(strpos($staffelpreis, ',') !== false)
		$staffelpreis = str_replace(",", ".", $staffelpreis);

	if(PRICE_IS_BRUTTO == 'true')
		$staffelpreis = ($staffelpreis / (get_tax_rate(get_tax_class_id($pid)) + 100) * 100);

	$staffelpreis = round($staffelpreis, PRICE_PRECISION);

	if($staffelpreis != '' && $quantity != '') {
		// ok, lets check entered data to get rid of user faults
		if($quantity <= 1)
			$quantity = 2;

		$check = $db->db_query("SELECT
									quantity
								FROM
									".TABLE_PERSONAL_OFFERS_BY.$staffelid."
								WHERE
									products_id = '".$pid."'
								AND
									quantity    = '".$quantity."'");
		// dont insert if same qty!
		if($check->_numOfRows < 1) {
			$db->db_query("INSERT INTO
								".TABLE_PERSONAL_OFFERS_BY.$staffelid."
							SET
								price_id       = '',
								products_id    = '".$pid."',
								quantity       = '".$quantity."',
								personal_offer = '".$staffelpreis."'");
			$error_message = '<div class="errorMessage">'.STAFFEL_ERROR_MESSAGE_2.'</div>';
			$ajax = update_staffel_table($pid,$ajax);
		} else {
			$error_message = '<div class="errorMessage">'.STAFFEL_ERROR_MESSAGE_3.'</div>';
		}
	} else {
		$error_message = '<div class="errorMessage">'.STAFFEL_ERROR_MESSAGE_4.'</div>';
	}

	$ajax->assign("staffel_add_".$staffelid,"innerHTML",$error_message);
	$ajax->assign('eingabebox_'.$staffelid, 'style.display', 'none');
	$ajax->assign('editbox_'.$staffelid, 'style.display', 'none');
	$ajax->assign("load_".$staffelid,"innerHTML",'');
	$ajax->assign("fehlermeldung_".$staffelpreis,"innerHTML",'');
	return $ajax;
}
?>